package top.lcm668.study.oj.base.sortmethods;

import java.util.Arrays;

/**
 * @author: Coder-Lcm
 * @date: 2021/10/2 20:32
 * 排序算法约定
 *      被排序类要求实现Comparable，因为要借助compareTo来进行比较
 */
public abstract class BaseSort<T extends Comparable<T>> {
    /**排序算法，交换的次数*/
    protected int swapCount = 0;
    /**排序算法，比较的次数*/
    protected int compareCount = 0;

    /**
     * 元素交换
     * @param arr
     * @param a
     * @param b
     */
    protected void swap(T[] arr, int a, int b) {
        swapCount++;
        T t = arr[a];
        arr[a] = arr[b];
        arr[b] = t;
    }

    protected boolean lessThan(T t1, T t2) {
        compareCount++;
        return t1.compareTo(t2) < 0;
    }

    protected boolean lessThanOrEquals(T t1, T t2) {
        compareCount++;
        return t1.compareTo(t2) <= 0;
    }

    protected boolean largeThan(T t1, T t2) {
        compareCount++;
        return t1.compareTo(t2) > 0;
    }

    /**
     * 目前仅用于基数排序的比较
     * @param t1
     * @param t2
     * @return
     */
    protected boolean largeThanForInteger(Integer t1, Integer t2) {
        compareCount++;
        return t1.compareTo(t2) > 0;
    }

    protected boolean largeThanOrEquals(T t1, T t2) {
        compareCount++;
        return t1.compareTo(t2) >= 0;
    }

    /**
     * 统一的排序方法，十大排序方法将来重写该方法即可
     * @param arr
     * @param isAsc 默认升序排列
     */
    protected abstract void sort(T[] arr, boolean isAsc);

    protected void print(T[] arr) {
        int len = arr.length;
        System.out.println("数据规模(n): " + len);
        System.out.print("排序后结果: ");
        for (int i = 0; i < len; i++) {
            System.out.print(arr[i] + ", ");
        }
        System.out.println();
        System.out.println("比较的次数(compare count): " + compareCount);
        System.out.println("交换的次数(swap count): " + swapCount);
    }

}
